/**
 */
package com.dingpeikeji.quickdeer.core.modules.buse.service;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.dingpeikeji.quickdeer.core.common.persistence.Page;
import com.dingpeikeji.quickdeer.core.common.service.CrudService;
import com.dingpeikeji.quickdeer.core.common.utils.StringUtils;
import com.dingpeikeji.quickdeer.core.modules.buse.dao.TBuseExchgeDao;
import com.dingpeikeji.quickdeer.core.modules.buse.dao.TCompBuseDao;
import com.dingpeikeji.quickdeer.core.modules.buse.entity.TBuseExchge;
import com.dingpeikeji.quickdeer.core.modules.buse.entity.TCompBuse;

/**
 * 巴士管理Service
 * @author Zhaoy
 * @version 2016-05-26
 */
@Service
@Transactional(readOnly = true)
public class TCompBuseService extends CrudService<TCompBuseDao, TCompBuse> {

	@Autowired
	private TBuseExchgeDao tBuseExchgeDao;
	
	public TCompBuse get(String id) {
		TCompBuse tCompBuse = super.get(id);
		tCompBuse.setTBuseExchgeList(tBuseExchgeDao.findList(new TBuseExchge(tCompBuse)));
		return tCompBuse;
	}
	
	public List<TCompBuse> findList(TCompBuse tCompBuse) {
		return super.findList(tCompBuse);
	}
	
	public Page<TCompBuse> findPage(Page<TCompBuse> page, TCompBuse tCompBuse) {
		return super.findPage(page, tCompBuse);
	}
	
	@Transactional(readOnly = false)
	public void save(TCompBuse tCompBuse) {
		super.save(tCompBuse);
		for (TBuseExchge tBuseExchge : tCompBuse.getTBuseExchgeList()){
			/*if (StringUtils.isEmpty(tBuseExchge.getId())){
				continue;
			}*/
			if (TBuseExchge.DEL_FLAG_NORMAL.equals(tBuseExchge.getDelFlag())){
				if (StringUtils.isBlank(tBuseExchge.getId())){
					tBuseExchge.setBusid(tCompBuse);
					tBuseExchge.preInsert();
					tBuseExchgeDao.insert(tBuseExchge);
				}else{
					tBuseExchge.preUpdate();
					tBuseExchgeDao.update(tBuseExchge);
				}
			}else{
				tBuseExchgeDao.delete(tBuseExchge);
			}
		}
	}
	
	@Transactional(readOnly = false)
	public void delete(TCompBuse tCompBuse) {
		super.delete(tCompBuse);
		tBuseExchgeDao.delete(new TBuseExchge(tCompBuse));
	}
	
}
